<template>
  <ui-page class="container">
    <!-- table -->
    <ui-table v-bind="table.config" ref="table" class="content_container" @select-change="onSelectTableEntity"
              :params="{tenantId: userInfo.tenantId, status: 1}">
      <!--   查询   -->
      <template v-slot:search-bar>
        <ui-table-select v-model="table.model.select" :reset="true">
          <ui-table-select-item name="employeeId" label="员工编号" placeholder="请输入员工编号" show-label type="number"
                                start-null/>
          <ui-table-select-item name="name" label="姓名" placeholder="请输入姓名" show-label type="text"
                                :show-word-limit="false"/>
          <ui-table-select-item name="departmentId" label="部门" placeholder="请选择部门" show-label type="select"
                                start-null url="/Ent/EntDepartment/selectList" :params="{tenantId: userInfo.tenantId}"
                                :props="{value: 'id', label: 'name'}"/>
          <ui-table-select-item name="transportation" label="出行类型" placeholder="请选择出行类型" show-label type="select"
                                url="Sys/SysDict/selectListChildren" :params="{code: 'GOOUT_TYPE'}" start-null/>
          <ui-table-select-item name="goOutType" label="外出类型" placeholder="请选择外出类型" show-label type="select"
                                :data="goOutType" start-null/>
          <ui-table-select-item name="startTime" label="开始时间" placeholder="请选择开始时间" show-label type="date"
                                start-null/>
          <ui-table-select-item name="endTime" label="结束时间" placeholder="请选择结束时间" show-label type="date"
                                start-null/>
        </ui-table-select>
      </template>
      <!--   操作   -->
      <template v-slot:tool-bar>
        <el-button type="primary" icon="el-icon-printer" @click="handleEntityExport">导出</el-button>
      </template>
      <ui-table-column label="序号" type="index" width="60px"/>
      <ui-table-column prop="employeeId" label="员工编号" min-width="120"/>
      <ui-table-column prop="entName" label="姓名" min-width="120"/>
      <ui-table-column prop="depName" label="部门" min-width="120"/>
      <ui-table-column prop="transportationText" label="出行类型" min-width="120"/>
      <ui-table-column prop="goOutAddress" label="外出地点" min-width="200"/>
      <ui-table-column prop="goOutText" label="外出类型" min-width="120"/>
      <ui-table-column prop="startTime" label="开始时间" min-width="200"/>
      <ui-table-column prop="endTime" label="结束时间" min-width="200"/>
      <ui-table-column prop="goOutDuration" label="外出时长/h" min-width="120">
        <template v-slot:default="{row}">
          {{ row.goOutDuration ? (row.goOutDuration / 3600).toFixed(2) : '-' }}
        </template>
      </ui-table-column>
      <ui-table-column prop="goOutReason" label="外出事由" min-width="200"/>
      <ui-table-column prop="managerName" label="部门审批" min-width="120"/>
      <ui-table-column prop="supervisorName" label="主管审批" min-width="120"/>
      <!--   分页   -->
      <template v-slot:footer-bar>
        <ui-table-pagination/>
      </template>
    </ui-table>
  </ui-page>
</template>
<script>
import {mapState} from 'vuex';
import to from "await-to-js";

const GoOutRecord = {
  API: {
    create: '/Go/GoOutRecord/create',
    update: '/Go/GoOutRecord/update',
    delete: '/Go/GoOutRecord/delete',
    select: '/Go/GoOutRecord/select',
    export: '/Go/GoOutRecord/export',
    createList: '/Go/GoOutRecord/createList',
    updateList: '/Go/GoOutRecord/updateList',
    deleteList: '/Go/GoOutRecord/deleteList',
    selectList: '/Go/GoOutRecord/selectList',
    selectPage: '/Go/GoOutRecord/selectPage',
  },
  data() {
    return {
      api: GoOutRecord.API,
      table: {
        config: {
          url: GoOutRecord.API.selectPage,
        },
        model: {
          select: {},
        },
      },
      form: {
        isCreate: true,
        title: '新增',
        url: GoOutRecord.API.create,
        visible: false,
        detailVisible: false,
        rules: {},
      },
      goOutType: [{value: 1, label: '因公'}, {value: 2, label: '因私'}],
    };
  },
  computed: {
    ...mapState(['userInfo']),
  },
  methods: {
    onSelectTableEntity(selects) {
      if (selects.length <= 0) {
        return;
      }
      this.handleEntityUpdateRow(selects[selects.length - 1]);
    },
    handleEntityCreate() {
      this.form.isCreate = true;
      this.$refs.form.resetValues();
      this.form.title = '新增';
      this.form.url = this.api.create;
      this.form.visible = true;
    },
    handleEntityUpdate() {
      const sels = this.$refs.table.getSelection();
      if (sels.length !== 1) {
        this.$message.info('请选择要修改的数据');
        return;
      }
      this.handleEntityUpdateRow(sels[0]);
    },
    handleEntityDelete() {
      const sels = this.$refs.table.getSelection();
      if (sels.length <= 0) {
        this.$message.info('请选择要删除的数据');
        return;
      }
      this.deleteEntities(sels.map(item => item.id)).then(({success}) => {
        if (!success) {
          return;
        }
        this.reloadTable();
      });
    },
    handleEntityUpdateRow(row) {
      this.form.isCreate = false;
      this.form.title = '修改';
      this.form.url = this.api.update;
      this.form.visible = true;

      this.selectEntity(row.id).then(({success, data}) => {
        if (!success) {
          this.form.visible = false;
          return;
        }
        console.log(data)
        this.$refs.form.resetValues(data);
      });
    },
    handleEntityDeleteRow(row) {
      this.deleteEntity(row.id).then(({success}) => {
        if (!success) {
          return;
        }
        this.reloadTable();
      });
    },
    async handleEntityExport() {
      const loading = this.$loading({text: '数据加载中..'});
      const url = this.api.export;
      // await this.$refs.table.exportEntity(url, '导出.xls');
      await this.exportEntity(url, '外出记录.xls');
      loading.close();
    },
    exportEntity(url, filename) {
      if (!url) {
        console.error(`请指定 export api 请求地址`);
        return;
      }
      const searchParams = this.$refs.table.getSearchParams();
      this.$axios.download(url, filename, {
        params: {...searchParams}
      });
    },
    reloadTable() {
      this.$refs.table && this.$refs.table.reload();
    },
    afterFormSubmit({success}) {
      if (!success) {
        return;
      }
      this.form.visible = false;
      this.reloadTable();
    },
  },
};
export default GoOutRecord;
</script>
<style lang="less" scoped>
</style>
